package com.eclink.asset.consume.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.consume.domain.dao.ConsumeCategory;
import com.eclink.asset.consume.domain.dao.ConsumeFile;
import com.eclink.asset.consume.mapper.ConsumeFileMapper;
import com.eclink.asset.pojo.query.AssetItemQuery;
import com.eclink.asset.sys.service.SysUserService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资产清单表Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class ConsumeFileService {

    @Resource
    private ConsumeFileMapper consumeFileMapper;

    @Resource
    private ConsumeCategoryService consumeCategoryService;

    @Resource
    private SysUserService userService;

    private static final String PREV_CODE = "HC";

    private static final int NUMBER_APPEND_LENGTH = 6;

    private String getZeroString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < NUMBER_APPEND_LENGTH; i++) {
            sb.append("0");
        }
        return sb.toString();
    }

    private String getNumber() {

        String numberPrefix = PREV_CODE;

        QueryWrapper<ConsumeFile> qw = new QueryWrapper<>();
        qw.likeRight("number", numberPrefix);
        qw.last("limit 1");
        qw.orderByDesc("number");
        ConsumeFile item = consumeFileMapper.selectOne(qw);
        int orderNo = 1;

        if (item != null) {
            String lastNumber = item.getNumber();
            int numberLength = lastNumber.length();
            orderNo = Integer.parseInt(lastNumber.substring(numberLength - NUMBER_APPEND_LENGTH, numberLength)) + 1;
        }
        String numberAppend = getZeroString() + orderNo;
        numberAppend = numberAppend.substring(numberAppend.length() - NUMBER_APPEND_LENGTH);

        return numberPrefix + numberAppend;
    }

    /**
     * 查询资产清单表
     *
     * @param id 资产清单表主键
     * @return 资产清单表
     */
    public ConsumeFile selectById(Long id) {
        ConsumeFile res = consumeFileMapper.selectById(id);
        if (res != null) {
            ConsumeCategory cate = consumeCategoryService.selectById(res.getCategoryId());
            if (cate != null) {
                res.setCategoryName(cate.getName());
            }
        }

        return res;
    }

    /**
     * 查询资产清单表列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 资产清单表
     */
    public List<ConsumeFile> selectList(AssetItemQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<ConsumeFile> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);

        if (query.getState()!=null) {
            qw.eq("state",query.getState());
        }
        if (StringUtils.isNotBlank(query.getValue())) {
            qw.like(query.getField(),query.getValue());
        }
        List<ConsumeFile> list = consumeFileMapper.selectList(qw);
        this.fillList(list);
        return list;
    }

    private void fillList(List<ConsumeFile> list) {
        for(ConsumeFile item : list) {
            ConsumeCategory cate = consumeCategoryService.selectById(item.getCategoryId());
            if (cate != null) {
                item.setCategoryName(cate.getName());
            }

        }
    }

    /**
     * 新增资产清单表
     *
     * @param assetItem 资产清单表
     * @return 结果
     */
    public int insert(ConsumeFile assetItem) {
        assetItem.setDelFlag(0);
        if (StringUtils.isBlank(assetItem.getNumber())) {
            assetItem.setNumber(getNumber());
        }
        return consumeFileMapper.insert(assetItem);
    }

    /**
     * 修改资产清单表
     *
     * @param assetItem 资产清单表
     * @return 结果
     */
    public int update(ConsumeFile assetItem) {
        return consumeFileMapper.updateById(assetItem);
    }

    /**
     * 批量删除资产清单表
     *
     * @param ids 需要删除的资产清单表主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        for(Long id : ids) {
            return consumeFileMapper.deleteById(id);
        }
        return 1;
    }

    /**
     * 删除资产清单表信息
     *
     * @param id 资产清单表主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return consumeFileMapper.deleteById(id);
    }
}
